//
//  WXBaMeiLableViewController.m
//  Big Show
//
//  Created by dlios on 15-5-18.
//  Copyright (c) 2015年 Big_Q. All rights reserved.
//

#import "XHbameiViewController.h"
#import "XH8meiTableViewCell.h"

@interface XHbameiViewController ()<UITableViewDataSource, UITableViewDelegate>


@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *mArr;


@end

@implementation XHbameiViewController

- (void)dealloc
{
    [_tableView release];
    [_mArr release];
    [_mDic release];
    [super dealloc];
}





- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    
    [self getData];
    [self creatrTableView];
    
#pragma mark - 收藏按钮
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"喜爱1"] style:UIBarButtonItemStylePlain target:self action:@selector(Preserve)] autorelease];
}

// 收藏按钮
-(void)Preserve
{
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    // 创建一个文件管理器
    NSFileManager *fileManager = [NSFileManager defaultManager];
    // 添加文件夹
    // 拼接文件夹地址
    NSString *filePath = [path stringByAppendingPathComponent:@"kof"];
    // 创建文件夹
    [fileManager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
    NSLog(@"%@", filePath);
    NSString *strPath = [filePath stringByAppendingPathComponent:@"kof.duang"];
    NSMutableArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile:strPath];
    
    if (arr == nil) {
        arr = [NSMutableArray array];
        [arr addObject:_mDic];
        // 归档
        [NSKeyedArchiver archiveRootObject:arr toFile:strPath];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"收藏成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
    } else {
        int a = 0;
        
        for (NSDictionary *dic in arr) {
            if ([[_mDic objectForKey:@"title"] isEqualToString:[dic objectForKey:@"title"]]) {
                a = 1;
            }
        }
        if (a == 0) {
            [arr addObject:_mDic];
            [NSKeyedArchiver archiveRootObject:arr toFile:strPath];
            
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"收藏成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
            [alert release];
        } else {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"已收藏" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
            [alert release];
        }
    }
}

- (void)getData
{
    
        self.mArr = [_mDic objectForKey:@"content"];
        
}

// webView上的链接不能点击跳转
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    //判断是否是单击
    if (navigationType == UIWebViewNavigationTypeLinkClicked)
    {
        
        return NO;
    }
    return YES;
}


- (void)creatrTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 113) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self.view addSubview:_tableView];
    [_tableView release];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _mArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    XH8meiTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[XH8meiTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    cell.label.text = [_mArr objectAtIndex:indexPath.row];
    return cell;
}










-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat h = [XH8meiTableViewCell heightWithText:[_mArr objectAtIndex:indexPath.row]];
    return h;
}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
